package com.cd.university.config.security.validate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;

/**
 * @author 陈栋
 * @create 2021/9/5 20:54
 */
@Component("validateCodeSecurityConfig")
public class ValidateCodeSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    /**
     * 这里将自动注入自定义的ValidateCodeFilter类
     * 因为ValidateCodeFilter类继承OncePerRequestFilter，
     * OncePerRequestFilter继承GenericFilterBean
     * GenericFilterBean继承Filter类
     * 所以这里注入相当于是以下语句:
     * @Autowired
     * ValidateCodeFilter validateCodeFilter;
     */
    @Autowired
    private Filter validateCodeFilter;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
